package com.itheima;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ServlrtConfigDemo extends HttpServlet {
    //生命Config对象
    private ServletConfig config;

    @Override
    public void init() throws ServletException {
        this.config = config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //1.演示Config方法
        //根据建获取值
        String encodingValue = config.getInitParameter("encoding");
        System.out.println(encodingValue);
        //2.获取所有的key
        Enumeration<String> keys = config.getInitParameterNames();//所有key
        while (keys.hasMoreElements()){
            String key = keys.nextElement();//每一个key
            String value = config.getInitParameter(key);
            System.out.println(key+","+value);
        }
        //3.获取Servlet名称
        String servletName = config.getServletName();
        System.out.println("servletName = " + servletName);
        //获取ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println("servletContext = " + servletContext);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
